package com.gitee.codegen.util;

import com.gitee.codegen.entity.ColumnEntity;
import com.gitee.codegen.entity.TableEntity;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.SQLException;
import java.util.List;

public class MysqlUtils {
    public static TableEntity getTableEntity(final QueryRunner runner, String tableName) throws SQLException {
        return runner.query("select table_name tableName, table_comment tableComment from information_schema.tables where `TABLE_SCHEMA` = (select database()) and `TABLE_NAME` = ?",
                new BeanHandler<>(TableEntity.class), tableName);
    }
    
    public static List<ColumnEntity> getColumnEntityList(final QueryRunner runner, String tableName) throws SQLException {
        return runner.query("select column_name columnName, data_type dataType, column_comment comments, column_key columnKey, extra, CHARACTER_MAXIMUM_LENGTH characterMaximumLength, NUMERIC_PRECISION numericPrecision, NUMERIC_SCALE numericScale from information_schema.columns " +
                "where table_name = ? and table_schema = (select database()) order by ordinal_position", new BeanListHandler<>(ColumnEntity.class), tableName);
    }
}
